<script setup>
import { defineOptions, ref, onMounted, inject } from 'vue'
import { useConfigStore } from '@/stores/config.js'
import { generatorConfig } from '@/utils/config.js'
import { ElMessage } from 'element-plus'

const configStore = useConfigStore()
const closeDialog = inject('closeDialog')
defineOptions({
  name: 'GeneratorConfig'
})
onMounted(() => {
  let savedConfig = ''
  if (window.utools) {
    // uTools 的环境，从 uTools 的数据库中获取配置信息
    savedConfig = JSON.parse(window.utools.dbStorage.getItem('config'))
  } else {
    // 非 uTools 环境，从Pinia中获取配置信息
    savedConfig = configStore.config
  }
  if (savedConfig) {
    form.value = savedConfig
  }
})

const form = ref({
  author: 'eleven',
  superClass: 'net.lesscoding.common.BaseEntity',
  tablePrefix: 'tb_',
  fieldPrefix: '',
  basePackage: 'net.lesscoding',
  dateType: 'LocalDateTime',
  lombok: true,
  swagger: 'knife4j',
  page: 'MybatisPage',
  exportFlag: false
})
const saveConfig = () => {
  if (window.utools) {
    // uTools 的环境，执行 uTools 的保存命令
    window.utools.dbStorage.setItem('config', JSON.stringify(form.value))
  }
  // 将数据保存到Pinia中
  configStore.refreshConfig(form.value)
  ElMessage.success('保存成功')
}

</script>
<template>
  <el-form :model="form" label-width="80">
    <el-row>
      <el-col :span="12">
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="作者"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="父类" prop="superClass">
          <el-input v-model="form.superClass" placeholder="父类"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="表前缀" prop="tablePrefix">
          <el-input v-model="form.tablePrefix" placeholder="表前缀"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="字段前缀" prop="fieldPrefix">
          <el-input v-model="form.fieldPrefix" placeholder="字段前缀"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="基础包名" prop="basePackage">
          <el-input v-model="form.basePackage" placeholder="基础包名"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="时间类型" prop="dateType">
          <el-select
              v-model="form.dateType"
              placeholder="请选择时间类型"
              filterable
          >
            <el-option
                v-for="(item, index) in generatorConfig.dateTypeOptions"
                :key="index"
                :label="item"
                :value="item"
            />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="Lombok" prop="lombok">
          <el-select
              v-model="form.lombok"
              filterable
          >
            <el-option
                v-for="(item, index) in generatorConfig.switchOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="Swagger" prop="swagger">
          <el-select
              v-model="form.swagger"
              filterable
          >
            <el-option
                v-for="(item, index) in generatorConfig.swaggerOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="分页插件" prop="page">
          <el-select
              v-model="form.page"
              filterable
          >
            <el-option
                v-for="(item, index) in generatorConfig.pager"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="导入导出" prop="export">
          <el-select
              v-model="form.exportFlag"
              filterable
          >
            <el-option
                v-for="(item, index) in generatorConfig.switchOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="3">
        <el-button @click="saveConfig" type="primary">保存</el-button>
      </el-col>
      <el-col :span="3">
        <el-button @click="closeDialog" type="primary">关闭</el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<style></style>
